<?php

namespace IDock\Insurance\CHAC\Request;

use IDock\Http\Request;
use IDock\Http\RequestFormat;
use IDock\Http\RequestMethod;

class AllCHACRequest extends Request
{

    protected $method = RequestMethod::POST;

    protected $format = RequestFormat::FORM;

    protected $inLog = true;

    protected $responseLog = true;

    public function loadConfig(array $config): Request
    {
        $this->gateway = $config['gateway'] ?? '';

        $this->logsDir = $config['logsDir'] ?? '';

        $this->timeout = $config['timeout'] ?? '';

        $this->proxy = $config['proxy'] ?? '';

        $this->proxyPW = $config['proxyPW'] ?? '';

        $this->proxyAccount = $config['proxyAccount'] ?? '';

        return $this;
    }

    public function requestLogs($uuid, $inLog = true)
    {
        if (!$this->logsFile) {
            return;
        }

        if ($this->urlTip) {
            $url = $this->gateway;
            $urlLogs = date('Y-m-d H:i:s') . " FUN_TIPS " . $uuid . " " . $this->urlTip . ' ' . $url;
        } else {
            $urlLogs = date('Y-m-d H:i:s') . " URL      " . $uuid . " " . $this->gateway;
        }

        $isTest = $GLOBALS['testResponse'] ?? '';

        if ($isTest and $urlLogs) {
            file_put_contents($this->logsFile, $urlLogs . "\r\n", FILE_APPEND);

            return;
        }

        file_put_contents($this->logsFile, "\r\n", FILE_APPEND);

        file_put_contents($this->logsFile, $urlLogs . "\r\n", FILE_APPEND);

        //header 日记
        if ($this->headers) {
            $encLogs = date('Y-m-d H:i:s') . " HEADER   " . $uuid . " " . json_encode($this->getHeaders(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            file_put_contents($this->logsFile, $encLogs . "\r\n", FILE_APPEND);
        }

        if (!$inLog) {
            return;
        }
        $biz = $this->getBizParam();

        if ($this instanceof FUploadFileRequest) {
            $biz['fileContent'] = '...';
        }

        if (is_array($biz)) {
            $biz = json_encode($biz, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        }

        $reqLogs = date('Y-m-d H:i:s') . " REQUEST  " . $uuid . " " . $biz;

        file_put_contents($this->logsFile, $reqLogs . "\r\n", FILE_APPEND);
    }

    public function responseLogs($uuid, $data)
    {

    }

    public function responseAfterLogs($data, $isSuccess)
    {
        if (!$this->logsFile) {
            return;
        }

        $uuid = $this->getRequestId();

        if (is_array($data)) {
            $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        }

        $repLogs = date('Y-m-d H:i:s') . " RESPONSE " . $uuid . " " . $data;

        file_put_contents($this->logsFile, $repLogs . "\r\n", FILE_APPEND);
    }

    public function errorLogs($uuid,$data)
    {
        if (!$this instanceof FUploadFileRequest) {
            return;
        }

        if (is_array($data)) {
            $data = http_build_query($data);
        }

        $reqLogs = date('Y-m-d H:i:s') . " ERROR    " . $uuid . " " . $data;

        file_put_contents($this->logsFile, $reqLogs . "\r\n", FILE_APPEND);
    }
}
